 package com.zu.commu.auto1_0.protocol.charge;

 import com.zu.commu.auto1_0.DataType1_0;
    import com.zu.commu.auto1_0.protocol.CheckData;
    import com.zu.commu.auto1_0.protocol.Util;
    import com.zu.commu.exception.CommuException;
    import com.zu.commu.interf.Data;

 public class ReadChargeDataProtocol
 {
   public Data analyseReportData(byte[] b)
     throws Exception
   {
     new CheckData().checkCrc(b, 4, 23, 24);
     Data d = new Data();
     getMeterNo(b, d);
     getWaterAmount(b, d);
     getChargeTimes(b, d);
     getChargeDate(b, d);
     d.setDataType(DataType1_0.READCHARGE.toInt()+"");
     return d;
   }

   private void getMeterNo(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       d.setMeterNo(u.byte2Hex(b, 4, 9));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取测控器地址号时出错！" +
         e.getMessage(), null);
     }
   }

   private void getWaterAmount(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       double dd = u.string2Double(u.byte2Hex(b, 12, 15));
       if (dd > 10000000.0D) {
         dd = 10000000.0D - dd;
       }
       d.setChargeAmount(Double.valueOf(dd));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取充值水量时出错！" +
         e.getMessage(), null);
     }
   }

   private void getChargeTimes(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       d.setChargeTimes(Integer.valueOf(u.string2Int(u.byte2Hex(b, 16, 17))));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取充值次数时出错！" +
         e.getMessage(), null);
     }
   }

   private void getChargeDate(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       d.setChargeDate(u.byte2Hex(b, 18, 23));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取充值时间时出错！" +
         e.getMessage(), null);
     }
   }
 }
